package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.entity.MallOrderEntity;
import com.cheer.entity.MallShopEntity;
import com.cheer.enums.MallProductStatusEnum;
import com.cheer.exception.ServiceException;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.MallOrderService;
import com.cheer.service.MallProductService;
import com.cheer.service.MallShopService;
import com.cheer.service.OaUserService;
import com.cheer.vo.MallAdminHomeVO;
import com.cheer.vo.MallShopHomeVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.cheer.enums.MallOrderStatusEnum.FINISHED;
import static com.cheer.enums.MallOrderStatusEnum.TO_DELIVER;

/**
 * @author cheer
 */
@RestController
public class MallHomeController {

    @Autowired
    private OaUserService oaUserService;

    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private MallProductService mallProductService;

    @Autowired
    private MallShopService mallShopService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @GetMapping("/admin/home")
    public Result<MallAdminHomeVO> adminHome() {
        MallAdminHomeVO adminHomeVO = new MallAdminHomeVO()
                .setMemberCount(oaUserService.count())
                .setProductCount(mallProductService.passedAndRemovedCount())
                .setReviewCount(mallProductService.reviewCount());

        List<MallOrderEntity> orderList = mallOrderService.listByStatus(Lists.newArrayList(FINISHED.getCode()));
        int orderCount = 0;
        int creditCount = 0;
        int creditMemberCount = 0;
        Set<Integer> userIdSet = new HashSet<>();
        BigDecimal money = new BigDecimal(0);
        for (MallOrderEntity order : orderList) {
            orderCount++;
            if (order.getCredit() != null) {
                creditCount += order.getCredit() * order.getQuantity();
                if (!userIdSet.contains(order.getUserId())) {
                    creditMemberCount++;
                    userIdSet.add(order.getUserId());
                }
            }
            if (order.getPrice() != null) {
                money = money.add(order.getPrice().multiply(new BigDecimal(order.getQuantity())));
            }
        }
        adminHomeVO.setOrderCount(orderCount)
                .setMoney(money.stripTrailingZeros().toPlainString())
                .setCreditCount(creditCount)
                .setCreditMemberCount(creditMemberCount);

        return Result.success(adminHomeVO);
    }

    @GetMapping("/admin/order")
    public Result<MallShopHomeVO> adminOrder(@RequestParam Long shopId) {
        return getOrderHomeByShopId(shopId);
    }

    @GetMapping("/shop/order")
    public Result<MallShopHomeVO> shopOrder() {
        Long shopId = userInfoHolder.getShopId();
        if (shopId == null) {
            throw new ServiceException("商户不存在");
        }

        return getOrderHomeByShopId(shopId);
    }

    @GetMapping("/shop/type")
    public Result<Integer> shopType() {
        Long shopId = userInfoHolder.getShopId();
        if (shopId == null) {
            throw new ServiceException("商户不存在");
        }

        MallShopEntity mallShopEntity = mallShopService.getById(shopId);
        if (mallShopEntity == null) {
            throw new ServiceException("商户不存在");
        }

        return Result.success(mallShopEntity.getType());
    }

    private Result<MallShopHomeVO> getOrderHomeByShopId(Long shopId) {
        MallShopHomeVO orderHomeVO = new MallShopHomeVO()
                .setUpCount(mallProductService.countByShopIdAndStatus(shopId, MallProductStatusEnum.PASSED.getCode()))
                .setDownCount(mallProductService.countByShopIdAndStatus(shopId, MallProductStatusEnum.REMOVED.getCode()));

        List<MallOrderEntity> orderList = mallOrderService.listByShopIdAndStatus(shopId, Lists.newArrayList(FINISHED.getCode()));
        BigDecimal money = new BigDecimal(0);
        int orderCount = 0;
        for (MallOrderEntity order : orderList) {
            orderCount++;
            if (order.getPrice() != null) {
                money = money.add(order.getPrice().multiply(new BigDecimal(order.getQuantity())));
            }
        }
        orderHomeVO.setToDeliverCount(mallOrderService.listByShopIdAndStatus(shopId, Lists.newArrayList(TO_DELIVER.getCode())).size())
                .setOrderCount(orderCount)
                .setMoney(money.stripTrailingZeros().toPlainString());

        return Result.success(orderHomeVO);
    }

}
